/*
 * @Date: 2025-07-16 21:00:19
 * @LastEditTime: 2025-07-26 21:29:17
 */
import { createRouter, createWebHashHistory } from "vue-router";
import Login from "../views/base/login.vue";
import Layout from "../views/main/layout.vue";
import Home from "../views/subpages/home/index.vue";
import Approval from "../views/subpages/approval/index.vue";
import Monitor from "../views/subpages/monitor/index.vue";
import { useAccountStore } from "../stores/account";
import { ElMessageBox } from "element-plus";

const router = createRouter({
  history: createWebHashHistory("/"),
  routes: [
    {
      path: "/",
      name: "login",
      component: Login,
    },
    {
      path: "/main",
      name: "main",
      redirect: "/main/home",
      component: Layout,
      children: [
        {
          path: "home",
          name: "home",
          component: Home,
          meta: {
            index: 1,
            title: "首页",
            icon: "HomeFilled",
          },
        },
        {
          path: "approval",
          name: "approval",
          component: Approval,
          meta: {
            index: 2,
            title: "注册审批",
            icon: "Checked",
          },
        },
        {
          path: "monitor",
          name: "monitor",
          component: Monitor,
          meta: {
            index: 3,
            title: "视频监控",
            icon: "VideoCameraFilled",
          },
        },
      ],
    },
    { path: "/:pathMatch(.*)*", redirect: "/" }, // 404页面
  ],
});

router.beforeEach((to, from, next) => {
  if (to.path.startsWith("/main")) {
    const accountStore = useAccountStore();
    if (accountStore.isExpired) {
      ElMessageBox({
        type: "warning",
        title: "提示",
        showClose: false,
        closeOnClickModal: false,
        closeOnPressEscape: false,
        message: "登录已过期，请重新登录",
      }).then(() => {
        accountStore.logout();
        router.replace("/login");
        next({
          path: "/login",
          replace: true,
        });
      });
    } else {
      next();
    }
  } else {
    next();
  }
});

export default router;
